package com.alit.face.koala.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.alit.face.koala.entity.KoalaCemara;
import com.alit.face.koala.repository.KoalaCemaraRepository;

/**
 * @author litong
 * @date 2019年5月10日_下午7:37:59
 * @version 1.0
 */
@RestController
@RequestMapping("koala_cemara")
public class KoalaCemaraController {
  @Autowired
  private KoalaCemaraRepository koalaCemaraRepository;

  @RequestMapping("list")
  public String list() {
    List<KoalaCemara> all = koalaCemaraRepository.findAll();
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("total", all.size());
    jsonObject.put("rows", all);
    return jsonObject.toJSONString();
  }

  @RequestMapping({ "save", "update" })
  public String save(KoalaCemara koalaCemara) {
    try {
      koalaCemaraRepository.save(koalaCemara);
      return "{'success':true}";
    } catch (Exception e) {
      e.printStackTrace();
      JSONObject jsonObject = new JSONObject(1);
      jsonObject.put("msg", e);
      return jsonObject.toJSONString();
    }
  }

  @RequestMapping("destroy")
  public String destroy(Integer id) {
    JSONObject jsonObject = new JSONObject(1);
    try {
      koalaCemaraRepository.deleteById(id);
      jsonObject.put("success", true);
    } catch (Exception e) {
      e.printStackTrace();
      jsonObject.put("msg", e);
    }
    return jsonObject.toJSONString();
  }

}
